package com.yu.crm.web;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yu.crm.domain.pojo.CrmDepartment;
import com.yu.crm.domain.pojo.CrmDepartmentPosition;
import com.yu.crm.service.CrmDepartmentService;
import com.yu.crm.service.impl.AttendanceSetService;
import com.yxzz.common.helper.valid.ValidatorHelpers;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.validation.Valid;
import java.util.List;

/**
 * <p>
 * 部门信息表 前端控制器
 * </p>
 *
 * @author donnie-bin
 * @since 2019-06-10
 */
@Api(tags = "部门管理")
@RestController
@RequestMapping("/crm-department")
public class CrmDepartmentController {

    @Autowired
    CrmDepartmentService departmentService;
    @Autowired
    AttendanceSetService attendanceSetService;

    @ApiOperation(value = "详情保存或修改")
    @PostMapping(value = "/save-or-update")
    public void saveOrUpdate(@RequestBody @Valid CrmDepartment department, BindingResult bindingResult){
        ValidatorHelpers.validateParams(bindingResult);
        departmentService.saveOrUpdate(department);
    }

    /**
     * 通过部门id当前部门及子项部门
     * @param id
     */
    @ApiOperation(value = "根据ID删除")
    @DeleteMapping(value = "/remove-by-id/{id}")
    public void removeById(@PathVariable Long id){
        ValidatorHelpers.notEmpty(id, "部门编号不能为空");
        departmentService.removeById(id);
    }

    @ApiOperation(value = "详情查询")
    @GetMapping(value = "/get-by-id/{id}")
    public CrmDepartment getById(@PathVariable Long id){
        return departmentService.getById(id);
    }

    @ApiOperation(value = "分页列表查询")
    @GetMapping(value = "/list")
    public IPage<CrmDepartment> list(Page<CrmDepartment> page, CrmDepartment queryInfo){
        return departmentService.list(page, queryInfo);
    }

    @ApiOperation(value = "树形列表查询")
    @GetMapping(value = "tree-list/{pid}")
    public List<CrmDepartment> treeList(@PathVariable Long pid){
        return departmentService.treeList(pid);
    }

    /**
     * 通过部门编号查询部门职位分页对象
     * @param page
     * @param id  部门编号
     * @param condition 条件
     * @return
     */
    @ApiOperation(value = "通过部门编号查询部门职位分页对象")
    @GetMapping(value = "/page-by-departmentId")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "current", value = "当前页", required = false, dataType = "int"),
            @ApiImplicitParam(name = "size", value = "页面大小", required = false, dataType = "int"),
            @ApiImplicitParam(name = "id", value = "部门编号", required = false, dataType = "Integer"),
            @ApiImplicitParam(name = "condition", value = "查询条件", required = false, dataType = "String")
    })
    public IPage<CrmDepartmentPosition> pageByDepartmentId(@ApiIgnore Page<CrmDepartmentPosition> page, Long id, String condition){
        //ValidatorHelpers.notEmpty(id, "部门编号不能为空");
        return departmentService.pageByDepartmentId(page,id,condition);
    }

    /**
     * 通过部门编号向上移动一位排序
     * @param id 部门编号
     */
    @ApiOperation(value = "通过部门编号向上移动一位排序")
    @GetMapping(value = "/update-sort/{id}")
    public void updateSort(@PathVariable Long id){
        ValidatorHelpers.notEmpty(id, "部门编号不能为空");
        departmentService.updateSort(id);
    }


    /**
     * 通过部门编号向上移动一位排序
     */
    @GetMapping(value = "aaa")
    public void aaa(){
        attendanceSetService.infoByUserId("SongJinQiao");
    }
}

